# -*- coding: utf-8 -*-
from bson import ObjectId
from config import DEFAULT_STUDIO_AVATAR_URL
from model import db, Collection
from time import time
import hashlib


class Studio(Collection):
    '''
* `_id` (string) - 画室 ID
* `answer` (list of string) - 画室的回答
* `answerNum` (int)
* `askMeList` (list of string) - 被提问的列表
* `article` (list of string)
* `articleNum` (int)
* `avatarUrl` (string) - 画室头像链接
* `createTime` (long) - 画室创建的时间
* `description` (string) - 画室的描述
* `email` (string) - 画室账号邮箱
* `environment` (list of string) - 画室环境图片
* `fan` (list of string) - 画室的粉丝列表
* `fanNum` (int)
* `follow` (list of string) - 画室的关注列表
* `followNum` (int)
* `name` (string) - 画室名称，该名称唯一
* `phone` (string) - 画室的号码
* `password` (string) - 用于登陆画室的密码
* `recruitment` (list of string) - 招聘消息
* `teacher` (list of string) - 画室的老师
* `teacherNum` (int)
* `video` (list of string) - 画室的视频
* `videoNum` (int)
* `register` (int) - 1 表示画室通过验证， 0 表示不通过
* `work` (lit of string) - 画室的作品
* `workNum` (int)
* `teacherIntro` (string)
    '''

    collection = db.studio

    class Field(object):
        _id = '_id'
        answer = 'answer'
        answerNum = 'answerNum'
        askMeList = 'askMeList'
        article = 'article'
        articleNum = 'articleNum'
        avatarUrl = 'avatarUrl'
        createTime = 'createTime'
        description = 'description'
        email = 'email'
        environment = 'environment'
        fan = 'fan'
        fanNum = 'fanNum'
        follow = 'follow'
        followNum = 'followNum'
        name = 'name'
        password = 'password'
        phone = 'phone'
        place = 'place'
        recruitment = 'recruitment'
        teacher = 'teacher'
        teacherNum = 'teacherNum'
        video = 'video'
        videoNum = 'videoNum'
        register = 'register'
        work = 'work'
        workNum = 'workNum'
        teacherIntro = 'teacherIntro'

    class Place(object):
        city = 'city'
        province = 'province'

    class Register(object):
        unverified = 0
        verified = 1

    collection.ensure_index(Field.email, sparse=True, unique=True)
    collection.ensure_index(Field.name, sparse=True, unique=True)
    collection.ensure_index(Field.phone, sparse=True, unique=True)

    @staticmethod
    def get_hashed_password(password):
        md5 = hashlib.md5()
        md5.update(password)
        hash_password = md5.hexdigest()
        return hash_password

    @staticmethod
    def new_studio(email, password):
        hash_password = Studio.get_hashed_password(password)
        return Studio.insert({
            Studio.Field.answer: [],
            Studio.Field.answerNum: 0,
            Studio.Field.askMeList: [],
            Studio.Field.article: [],
            Studio.Field.articleNum: 0,
            Studio.Field.avatarUrl: DEFAULT_STUDIO_AVATAR_URL,
            Studio.Field.createTime: int(time() * 1000),
            Studio.Field.description: '',
            Studio.Field.email: email,
            Studio.Field.environment: [],
            Studio.Field.fan: [],
            Studio.Field.fanNum: 0,
            Studio.Field.follow: [],
            Studio.Field.followNum: 0,
            Studio.Field.name: u'画室' + str(ObjectId()),
            Studio.Field.password: hash_password,
            Studio.Field.place: {
                Studio.Place.province: '',
                Studio.Place.city: ''
            },
            Studio.Field.recruitment: [],
            Studio.Field.teacher: [],
            Studio.Field.teacherNum: 0,
            Studio.Field.video: [],
            Studio.Field.videoNum: 0,
            Studio.Field.register: Studio.Register.verified,
            Studio.Field.work: [],
            Studio.Field.workNum: 0
        })
